package com.qingfeng.medical.biz.hosp.listener;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellData;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qingfeng.medical.biz.hosp.domain.entity.HospDepartment;
import com.qingfeng.medical.biz.hosp.domain.vo.HospDepartmentExcelVo;
import com.qingfeng.medical.biz.hosp.service.HospDepartmentService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 清风学Java
 * @version 1.0.0
 * @date 2023/4/10
 */
public class HospDepartmentListener extends AnalysisEventListener<HospDepartmentExcelVo> {

    private HospDepartmentService hospDepartmentService;

    /**
     * 每隔100条存储数据库，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    /**
     * 缓存的数据
     */
    private List<HospDepartment> cachedAddList = new ArrayList<>(BATCH_COUNT);
    private List<HospDepartment> cachedUpdateList = new ArrayList<>(BATCH_COUNT);


    /**
     * 使用有参的构造方法，传入需要的参数对象
     *
     * @param dictService
     */
    public HospDepartmentListener(HospDepartmentService dictService) {
        this.hospDepartmentService = dictService;
    }

    /**
     * 一行一行读取Excel内容，从第二行读取，第一行是表头不读取
     *
     * @param hospDepartmentExcelVo
     * @param analysisContext
     */
    @Override
    public void invoke(HospDepartmentExcelVo hospDepartmentExcelVo, AnalysisContext analysisContext) {
        //科室名和门诊名不能重复
        HospDepartment hospDepartment = hospDepartmentService.getOne(
                new LambdaQueryWrapper<HospDepartment>()
                        .eq(HospDepartment::getDepName, hospDepartmentExcelVo.getDepName())
                        .eq(HospDepartment::getPatientName, hospDepartmentExcelVo.getPatientName())
        );
        if (ObjectUtil.isEmpty(hospDepartment)) {
            //不存在，就可以直接进行存储
            cachedAddList.add(HospDepartment.builder()
                    .depCode(hospDepartmentExcelVo.getDepCode())
                    .depName(hospDepartmentExcelVo.getDepName())
                    .patientName(hospDepartmentExcelVo.getPatientName())
                    .patientCode(hospDepartmentExcelVo.getPatientCode())
                    .intro(hospDepartmentExcelVo.getIntro())
                    .status(hospDepartmentExcelVo.getStatus().equals("启用") ? 1 : 0)
                    .build());
        } else {
            //存在则更新
            hospDepartment.setDepName(hospDepartment.getDepName())
                    .setDepCode(hospDepartment.getDepCode())
                    .setPatientName(hospDepartmentExcelVo.getPatientName())
                    .setPatientCode(hospDepartmentExcelVo.getPatientCode())
                    .setIntro(hospDepartmentExcelVo.getIntro())
                    .setStatus(hospDepartmentExcelVo.getStatus().equals("启用") ? 1 : 0);
            cachedUpdateList.add(hospDepartment);

        }

        //执行
        if (cachedAddList.size() >= BATCH_COUNT) {
            hospDepartmentService.saveBatch(cachedAddList);
            cachedAddList.clear();

        }
        if (cachedUpdateList.size() >= BATCH_COUNT) {
            hospDepartmentService.updateBatchById(cachedUpdateList);
            cachedUpdateList.clear();
        }


    }

    /**
     * 读取Excel表头第一行的数据
     *
     * @param headMap
     * @param context
     */
    @Override
    public void invokeHead(Map<Integer, CellData> headMap, AnalysisContext context) {

    }

    /**
     * 读取Excel结束后调用
     *
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        if (CollUtil.isNotEmpty(cachedAddList)) {
            //确保最后的数据能够存储
            hospDepartmentService.saveBatch(cachedAddList);
        }

        if (CollUtil.isNotEmpty(cachedUpdateList)) {
            //确保最后的数据能够存储
            hospDepartmentService.updateBatchById(cachedUpdateList);

        }
    }
}
